
#include "ShaderElementBuffer.h"

ShaderElementBuffer::ShaderElementBuffer(ShaderElementDataPtr pElementData, ShaderVertexDataPtr pVertexData)
	: ShaderBuffer(pVertexData)
	, m_pElementData(pElementData)
	, m_veo(0)
{

}

void ShaderElementBuffer::initBinding()
{
	ShaderBuffer::initBinding();
	if (!m_pElementData)	return;

	glGenBuffers(1, &m_veo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_veo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_pElementData->getDataSize(), m_pElementData->getData(), GL_STATIC_DRAW);
}

void ShaderElementBuffer::cleanBinding()
{
	ShaderVertex::cleanBinding();
	if (m_veo > 0)
	{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
		glDeleteBuffers(1, &m_veo);
		m_veo = 0;
	}
	m_pElementData = nullptr;
}

void ShaderElementBuffer::draw(GLuint vertexSize, GLuint vertexIndex)
{
	glDrawElements(GL_TRIANGLES, vertexSize, GL_UNSIGNED_INT, 0);
}

